Hệ thống sổ địa chỉ đơn giản trong PHP
1 <?php
2
3 require './config.php';
4 $mode = $_REQUEST["mode"];
5 if ($mode == "add_new" ) {
6 $first_name = trim($_POST['first_name']);
7 $middle_name = trim($_POST['middle_name']);
8 $last_name = trim($_POST['last_name']);
9 $email_id = trim($_POST['email_id']);
10 $contact_no1 = trim($_POST['contact_no1']);
11 $contact_no2 = trim($_POST['contact_no2']);
12 $address = trim($_POST['address']);
13 $filename = "";
14 $error = FALSE;
15
16 if (is_uploaded_file($_FILES["profile_pic"]["tmp_name"])) {
17 $filename = time() . '_' . $_FILES["profile_pic"]["name"];
18 $filepath = 'profile_pics/' . $filename;
19 if (!move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $filepath)) {
20 $error = TRUE;
21 }
22 }
23
24 if (!$error) {
25 $sql = "INSERT INTO `tbl_contacts` (`first_name`, `middle_name`, `last_name`, `address`, `contact_no1`, `contact_no2`, `email_address`, `profile_pic`) VALUES "
26 . "( :fname, :mname, :lname, :address, :contact1, :contact2, :email, :pic)";
27
28 try {
29 $stmt = $DB->prepare($sql);
30
31 // bind the values
32 $stmt->bindValue(":fname", $first_name);
33 $stmt->bindValue(":mname", $middle_name);
34 $stmt->bindValue(":lname", $last_name);
35 $stmt->bindValue(":address", $address);
36 $stmt->bindValue(":contact1", $contact_no1);
37 $stmt->bindValue(":contact2", $contact_no2);
38 $stmt->bindValue(":email", $email_id);
39 $stmt->bindValue(":pic", $filename);
40
41 // execute Query
42 $stmt->execute();
43 $result = $stmt->rowCount();
44 if ($result > 0) {
45 $_SESSION["errorType"] = "success";
46 $_SESSION["errorMsg"] = "Contact added successfully.";
47 } else {
48 $_SESSION["errorType"] = "danger";
49 $_SESSION["errorMsg"] = "Failed to add contact.";
50 }
51 } catch (Exception $ex) {
52
53 $_SESSION["errorType"] = "danger";
54 $_SESSION["errorMsg"] = $ex->getMessage();
55 }
56 } else {
57 $_SESSION["errorType"] = "danger";
58 $_SESSION["errorMsg"] = "failed to upload image.";
59 }
60 header("location:index.php");
61 } elseif ( $mode == "update_old" ) {
62
63 $first_name = trim($_POST['first_name']);
64 $middle_name = trim($_POST['middle_name']);
65 $last_name = trim($_POST['last_name']);
66 $email_id = trim($_POST['email_id']);
67 $contact_no1 = trim($_POST['contact_no1']);
68 $contact_no2 = trim($_POST['contact_no2']);
69 $address = trim($_POST['address']);
70 $cid = trim($_POST['cid']);
71 $filename = "";
72 $error = FALSE;
73
74 if (is_uploaded_file($_FILES["profile_pic"]["tmp_name"])) {
75 $filename = time() . '_' . $_FILES["profile_pic"]["name"];
76 $filepath = 'profile_pics/' . $filename;
77 if (!move_uploaded_file($_FILES["profile_pic"]["tmp_name"], $filepath)) {
78 $error = TRUE;
79 }
80 } else {
81 $filename = $_POST['old_pic'];
82 }
83
84 if (!$error) {
85 $sql = "UPDATE `tbl_contacts` SET `first_name` = :fname, `middle_name` = :mname, `last_name` = :lname, `address` = :address, `contact_no1` = :contact1, `contact_no2` = :contact2, `email_address` = :email, `profile_pic` = :pic "
86 . "WHERE contact_id = :cid ";
87
88 try {
89 $stmt = $DB->prepare($sql);
90
91 // bind the values
92 $stmt->bindValue(":fname", $first_name);
93 $stmt->bindValue(":mname", $middle_name);
94 $stmt->bindValue(":lname", $last_name);
95 $stmt->bindValue(":address", $address);
96 $stmt->bindValue(":contact1", $contact_no1);
97 $stmt->bindValue(":contact2", $contact_no2);
98 $stmt->bindValue(":email", $email_id);
99 $stmt->bindValue(":pic", $filename);
100 $stmt->bindValue(":cid", $cid);
101
102 // execute Query
103 $stmt->execute();
104 $result = $stmt->rowCount();
105 if ($result > 0) {
106 $_SESSION["errorType"] = "success";
107 $_SESSION["errorMsg"] = "Contact updated successfully.";
108 } else {
109 $_SESSION["errorType"] = "info";
110 $_SESSION["errorMsg"] = "No changes made to contact.";
111 }
112 } catch (Exception $ex) {
113
114 $_SESSION["errorType"] = "danger";
115 $_SESSION["errorMsg"] = $ex->getMessage();
116 }
117 } else {
118 $_SESSION["errorType"] = "danger";
119 $_SESSION["errorMsg"] = "Failed to upload image.";
120 }
121 header("location:index.php?pagenum=".$_POST['pagenum']);
122 } elseif ( $mode == "delete" ) {
123 $cid = intval($_GET['cid']);
124
125 $sql = "DELETE FROM `tbl_contacts` WHERE contact_id = :cid";
126 try {
127
128 $stmt = $DB->prepare($sql);
129 $stmt->bindValue(":cid", $cid);
130
131 $stmt->execute();
132 $res = $stmt->rowCount();
133 if ($res > 0) {
134 $_SESSION["errorType"] = "success";
135 $_SESSION["errorMsg"] = "Contact deleted successfully.";
136 } else {
137 $_SESSION["errorType"] = "info";
138 $_SESSION["errorMsg"] = "Failed to delete contact.";
139 }
140
141 } catch (Exception $ex) {
142 $_SESSION["errorType"] = "danger";
143 $_SESSION["errorMsg"] = $ex->getMessage();
144 }
145
146 header("location:index.php?pagenum=".$_GET['pagenum']);
147 }
148 ?>